package com.kingdee.eas.custom.material.client;

import java.awt.Component;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.tree.TreeNode;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.swing.KDPromptBox;
import com.kingdee.bos.ctrl.swing.KDToolBar;
import com.kingdee.bos.ctrl.swing.tree.DefaultKingdeeTreeNode;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectStringPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.framework.cache.ActionCache;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemCollection;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.entity.SorterItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.workflow.ProcessInstInfo;
import com.kingdee.bos.workflow.service.ormrpc.EnactmentServiceFactory;
import com.kingdee.bos.workflow.service.ormrpc.IEnactmentService;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.base.permission.PermissionFactory;
import com.kingdee.eas.basedata.assistant.PeriodInfo;
import com.kingdee.eas.basedata.assistant.SystemStatusCtrolUtils;
import com.kingdee.eas.basedata.master.cssp.CSSPGroupInfo;
import com.kingdee.eas.basedata.master.cssp.SupplierFactory;
import com.kingdee.eas.basedata.master.cssp.UsedStatusEnum;
import com.kingdee.eas.basedata.org.CompanyOrgUnitInfo;
import com.kingdee.eas.basedata.org.CostCenterOrgUnitInfo;
import com.kingdee.eas.basedata.org.CtrlUnitCollection;
import com.kingdee.eas.basedata.org.CtrlUnitFactory;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.FullOrgUnitCollection;
import com.kingdee.eas.basedata.org.FullOrgUnitFactory;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgStructureInfo;
import com.kingdee.eas.basedata.org.OrgType;
import com.kingdee.eas.basedata.org.client.f7.AdminF7;
import com.kingdee.eas.basedata.person.client.PersonPromptBox;
import com.kingdee.eas.cm.common.client.GeneralKDPromptSelectorAdaptor;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.fdc.basedata.ContractTypeCollection;
import com.kingdee.eas.fdc.basedata.ContractTypeFactory;
import com.kingdee.eas.fdc.basedata.ContractTypeInfo;
import com.kingdee.eas.fdc.basedata.CurProjectCollection;
import com.kingdee.eas.fdc.basedata.CurProjectFactory;
import com.kingdee.eas.fdc.basedata.CurProjectInfo;
import com.kingdee.eas.fdc.basedata.ProjectWithCostCenterOUCollection;
import com.kingdee.eas.fdc.basedata.ProjectWithCostCenterOUFactory;
import com.kingdee.eas.fdc.basedata.ProjectWithCostCenterOUInfo;
import com.kingdee.eas.fdc.basedata.TargetTypeCollection;
import com.kingdee.eas.fdc.basedata.TargetTypeFactory;
import com.kingdee.eas.fdc.basedata.TargetTypeInfo;
import com.kingdee.eas.fdc.basedata.client.RespDeptDataChangeLisenter;
import com.kingdee.eas.fdc.contract.ContractBillEntryFactory;
import com.kingdee.eas.fdc.contract.ContractBillFactory;
import com.kingdee.eas.fdc.contract.ContractWithoutTextFactory;
import com.kingdee.eas.fdc.contract.client.ContractClientUtils;
import com.kingdee.eas.fi.gl.GlWebServiceUtil;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.SystemEnum;
import com.kingdee.eas.framework.client.CoreUI;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.ExceptionHandler;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.util.StringUtils;

//Referenced classes of package com.kingdee.eas.fdc.basedata.client:
//RespDeptDataChangeLisenter

public class FDCClientUtils {

	public FDCClientUtils() {
	}

	public static String getRes(String resName) {
		return EASResource.getString(
				"com.kingdee.eas.fdc.basedata.client.FdcResource", resName);
	}

	public static void clearSeparatorOnToolBar(KDToolBar toolBar) {
		int size = toolBar.getComponentCount();
		Component c = null;
		boolean isSeparator = false;
		int lastVisibleBtnPosition = 0;
		for (int i = 0; i < size; i++) {
			c = toolBar.getComponentAtIndex(i);
			if (!c.isVisible())
				continue;
			if (c instanceof javax.swing.JToolBar.Separator) {
				if (isSeparator)
					c.setVisible(false);
				isSeparator = true;
			} else {
				isSeparator = false;
				lastVisibleBtnPosition = i;
			}
		}

		for (int i = lastVisibleBtnPosition; i < size; i++) {
			c = toolBar.getComponentAtIndex(i);
			if (c instanceof javax.swing.JToolBar.Separator)
				c.setVisible(false);
		}

	}

	public static void checkProjUsed(CoreUIObject ui, String projId)
			throws Exception {
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems()
				.add(new FilterItemInfo("curProject.id", projId));
		if (ContractBillFactory.getRemoteInstance().exists(filter)
				|| ContractWithoutTextFactory.getRemoteInstance()
						.exists(filter)) {
			String warning = getRes("projUsed");
			MsgBox.showWarning(ui, warning);
			SysUtil.abort();
		}
	}

	public static void checkBillInWorkflow(CoreUIObject ui, String id) {
		ProcessInstInfo instInfo = null;
		ProcessInstInfo procInsts[] = null;
		try {
			IEnactmentService service2 = EnactmentServiceFactory
					.createRemoteEnactService();
			procInsts = service2.getProcessInstanceByHoldedObjectId(id);
		} catch (BOSException e) {
			ExceptionHandler.handle(e);
		}
		int i = 0;
		for (int n = procInsts.length; i < n; i++)
			if ("open.running".equals(procInsts[i].getState())
					|| "open.not_running.suspended".equals(procInsts[i]
							.getState()))
				instInfo = procInsts[i];

		if (instInfo != null) {
			MsgBox
					.showWarning(
							ui,
							EASResource
									.getString("com.kingdee.eas.framework.FrameWorkResource.Msg_BindWfInstance"));
			SysUtil.abort();
		}
	}

	public static void genLeafNodesIdSet(TreeNode node, Set leafNodesIdSet) {
		int count = node.getChildCount();
		if (count == 0) {
			OrgStructureInfo orgStructureInfo = (OrgStructureInfo) ((DefaultKingdeeTreeNode) node)
					.getUserObject();
			String oid = orgStructureInfo.getUnit().getId().toString();
			leafNodesIdSet.add(oid);
			return;
		}
		DefaultKingdeeTreeNode treeNode = null;
		for (int i = 0; i < count; i++) {
			treeNode = (DefaultKingdeeTreeNode) node.getChildAt(i);
			if (treeNode.isLeaf()) {
				String id = ((OrgStructureInfo) treeNode.getUserObject())
						.getUnit().getId().toString();
				leafNodesIdSet.add(id);
			} else {
				genLeafNodesIdSet(((TreeNode) (treeNode)), leafNodesIdSet);
			}
		}

	}

	public static void genNodesIdSet(TreeNode node, Set leafNodesIdSet) {
		int count = node.getChildCount();
		OrgStructureInfo orgStructureInfo = (OrgStructureInfo) ((DefaultKingdeeTreeNode) node)
				.getUserObject();
		String oid = orgStructureInfo.getUnit().getId().toString();
		leafNodesIdSet.add(oid);
		DefaultKingdeeTreeNode treeNode = null;
		for (int i = 0; i < count; i++) {
			treeNode = (DefaultKingdeeTreeNode) node.getChildAt(i);
			String id = ((OrgStructureInfo) treeNode.getUserObject()).getUnit()
					.getId().toString();
			leafNodesIdSet.add(id);
			if (!treeNode.isLeaf())
				genNodesIdSet(((TreeNode) (treeNode)), leafNodesIdSet);
		}

	}

	public static void genNodesIdSet2(TreeNode node, Set leafNodesIdSet) {
		int count = node.getChildCount();
		if (count == 0) {
			CoreBaseInfo info = (CoreBaseInfo) ((DefaultKingdeeTreeNode) node)
					.getUserObject();
			String oid = info.getId().toString();
			leafNodesIdSet.add(oid);
			return;
		}
		DefaultKingdeeTreeNode treeNode = null;
		for (int i = 0; i < count; i++) {
			treeNode = (DefaultKingdeeTreeNode) node.getChildAt(i);
			String id = ((CoreBaseInfo) treeNode.getUserObject()).getId()
					.toString();
			leafNodesIdSet.add(id);
			if (!treeNode.isLeaf())
				genNodesIdSet2(((TreeNode) (treeNode)), leafNodesIdSet);
		}

	}

	public static void genProjLeafNodesIdSet(TreeNode node, Set leafNodesIdSet) {
		int count = node.getChildCount();
		if (count == 0) {
			CurProjectInfo curProjectInfo = (CurProjectInfo) ((DefaultKingdeeTreeNode) node)
					.getUserObject();
			String oid = curProjectInfo.getId().toString();
			leafNodesIdSet.add(oid);
			return;
		}
		DefaultKingdeeTreeNode treeNode = null;
		for (int i = 0; i < count; i++) {
			treeNode = (DefaultKingdeeTreeNode) node.getChildAt(i);
			if (treeNode.isLeaf()) {
				if (treeNode.getUserObject() instanceof CurProjectInfo) {
					String id = ((CurProjectInfo) treeNode.getUserObject())
							.getId().toString();
					leafNodesIdSet.add(id);
				}
			} else {
				genProjLeafNodesIdSet(((TreeNode) (treeNode)), leafNodesIdSet);
			}
		}

	}

	public static boolean isAllowModifyNumber(IObjectValue info,
			String orgUnitId) throws BOSException, EASBizException {
		boolean result = false;
		ICodingRuleManager iCodingRuleManager = CodingRuleManagerFactory
				.getRemoteInstance();
		if (iCodingRuleManager.isExist(info, orgUnitId))
			return iCodingRuleManager.isModifiable(info, orgUnitId);
		try {
			return true;
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		return result;
	}

	public static void checkSelectProj(CoreUIObject ui,
			DefaultKingdeeTreeNode selectTreeNode) {
		if (selectTreeNode == null) {
			MsgBox.showWarning(ui, ContractClientUtils.getRes("selectProjPls"));
			SysUtil.abort();
		}
		if (!(selectTreeNode.getUserObject() instanceof CurProjectInfo)) {
			MsgBox
					.showWarning(ui, ContractClientUtils
							.getRes("selectProjPls2"));
			SysUtil.abort();
		}
		if (!selectTreeNode.isLeaf()) {
			MsgBox.showWarning(ui, ContractClientUtils
					.getRes("selectProjLeafPls"));
			SysUtil.abort();
		}
	}

	public static void checkProjWithCostOrg(CoreUIObject ui,
			DefaultKingdeeTreeNode selectTreeNode) {
		CurProjectInfo curProj = (CurProjectInfo) selectTreeNode
				.getUserObject();
		boolean exists = curProj.getCostCenter() != null;
		if (!exists) {
			MsgBox
					.showWarning(
							ui,
							"\u60A8\u6240\u9009\u62E9\u7684\u5DE5\u7A0B\u9879\u76EE\u6CA1\u6709\u5BF9\u5E94\u5230\u6210\u672C\u4E2D\u5FC3\uFF0C\u65E0\u6CD5\u7EE7\u7EED\uFF0C\u8BF7\u5148\u8BBE\u7F6E\u4E8C\u8005\u7684\u5BF9\u5E94\u5173\u7CFB");
			SysUtil.abort();
		}
	}

	public static boolean checkProjWithCostOrg(String projId) {
		boolean exists = false;
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems()
				.add(new FilterItemInfo("curProject.id", projId));
		try {
			exists = ProjectWithCostCenterOUFactory.getRemoteInstance().exists(
					filter);
		} catch (EASBizException e) {
			ExceptionHandler.handle(e);
		} catch (BOSException e) {
			ExceptionHandler.handle(e);
		}
		return exists;
	}

	private static String getTopProjId(String projId) {
		CurProjectInfo curProjectInfo = null;
		try {
			curProjectInfo = CurProjectFactory.getRemoteInstance()
					.getCurProjectInfo(new ObjectStringPK(projId));
		} catch (EASBizException e1) {
			ExceptionHandler.handle(e1);
		} catch (BOSException e1) {
			ExceptionHandler.handle(e1);
		}
		if (curProjectInfo.getParent() == null)
			return projId;
		String longNumber = curProjectInfo.getLongNumber();
		String pln = longNumber.substring(0, longNumber.indexOf("!"));
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(new FilterItemInfo("longNumber", pln));
		view.setFilter(fi);
		CurProjectCollection coll = null;
		try {
			coll = CurProjectFactory.getRemoteInstance()
					.getCurProjectCollection(view);
		} catch (BOSException e1) {
			ExceptionHandler.handle(e1);
		}
		String pProjId = null;
		if (coll.size() > 0) {
			CurProjectInfo pProjInfo = coll.get(0);
			pProjId = pProjInfo.getId().toString();
		}
		return pProjId;
	}

	public static FullOrgUnitInfo getCostOrgByProj(String projId) {
		String pProjId = checkProjWithCostOrg(projId) ? projId
				: getTopProjId(projId);
		FullOrgUnitInfo orgInfo = null;
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("curProject.id", pProjId));
		view.setFilter(filter);
		view.getSelector().add("costCenterOU.*");
		ProjectWithCostCenterOUCollection pwcc = null;
		try {
			pwcc = ProjectWithCostCenterOUFactory.getRemoteInstance()
					.getProjectWithCostCenterOUCollection(view);
		} catch (BOSException e) {
			ExceptionHandler.handle(e);
		}
		if (pwcc.size() > 0) {
			ProjectWithCostCenterOUInfo info = pwcc.get(0);
			orgInfo = info.getCostCenterOU().castToFullOrgUnitInfo();
		}
		return orgInfo;
	}

	public static void removeNoneCompanyNode(DefaultKingdeeTreeNode node) {
		for (int i = 0; i < node.getChildCount(); i++) {
			DefaultKingdeeTreeNode curNode = (DefaultKingdeeTreeNode) node
					.getChildAt(i);
			OrgStructureInfo orgStru = (OrgStructureInfo) curNode
					.getUserObject();
			if (!orgStru.getUnit().isIsCompanyOrgUnit()) {
				node.remove(i);
				i--;
				continue;
			}
			if (curNode.getChildCount() > 0)
				removeNoneCompanyNode(curNode);
		}

	}

	public static void showOprtOK(Component comp) {
		MsgBox.showWarning(comp, ContractClientUtils.getRes("oprateOK"));
	}

	public static void showOprtFailed(Component comp) {
		MsgBox.showWarning(comp, getRes("oprateFailed"));
	}

	public static Set genOrgUnitIdSet(BOSUuid id) throws Exception {
		Set idSet = new HashSet();
		FullOrgUnitInfo orgUnitInfo = FullOrgUnitFactory.getRemoteInstance()
				.getFullOrgUnitInfo(new ObjectUuidPK(id));
		String orgUnitLongNumber = orgUnitInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", orgUnitLongNumber + "%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", orgUnitLongNumber));
		filter.getFilterItems().add(
				new FilterItemInfo("isCostOrgUnit", Boolean.TRUE));
		filter.setMaskString("(#0 or #1) and #2");
		view.setFilter(filter);
		view.getSelector().add("id");
		FullOrgUnitCollection fullOrgUnitCollection = FullOrgUnitFactory
				.getRemoteInstance().getFullOrgUnitCollection(view);
		FullOrgUnitInfo element;
		for (Iterator iter = fullOrgUnitCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (FullOrgUnitInfo) iter.next();

		return idSet;
	}
	public static Set genProjectIdSet(CurProjectInfo cuInfo) throws Exception {
		Set idSet = new HashSet();
		CurProjectInfo curProjectInfo = cuInfo;
		String longNumber = curProjectInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber + "!%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber));
		filter.getFilterItems().add(
				new FilterItemInfo("CU", curProjectInfo
						.getCU().getId().toString()));
		filter.setMaskString("(#0 or #1) and #2");
		view.setFilter(filter);
		CurProjectCollection curProjectCollection = CurProjectFactory.getRemoteInstance().getCurProjectCollection(view);
		CurProjectInfo element;
		for (Iterator iter = curProjectCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (CurProjectInfo) iter.next();

		return idSet;
	}

	public static Set genCtrlUnitIdSet(CtrlUnitInfo cuInfo) throws Exception {
		Set idSet = new HashSet();
		CtrlUnitInfo curProjectInfo = cuInfo;
		String longNumber = curProjectInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber + "!%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber));
		filter.getFilterItems().add(
				new FilterItemInfo("CU", curProjectInfo
						.getCU().getId().toString()));
		filter.setMaskString("(#0 or #1) and #2");
		view.setFilter(filter);
		CtrlUnitCollection curProjectCollection = CtrlUnitFactory.getRemoteInstance().getCtrlUnitCollection(view);
		CtrlUnitInfo element;
		for (Iterator iter = curProjectCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (CtrlUnitInfo) iter.next();

		return idSet;
	}

	public static Set genProjectIdSet(BOSUuid id, boolean isOnlyLeaf)
			throws Exception {
		Set idSet = new HashSet();
		CurProjectInfo curProjectInfo = CurProjectFactory.getRemoteInstance()
				.getCurProjectInfo(new ObjectUuidPK(id));
		String longNumber = curProjectInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber + "!%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber));
		filter.getFilterItems().add(
				new FilterItemInfo("fullOrgUnit.id", curProjectInfo
						.getFullOrgUnit().getId().toString()));
		if (isOnlyLeaf) {
			filter.getFilterItems().add(
					new FilterItemInfo("isLeaf", Boolean.TRUE));
			filter.setMaskString("(#0 or #1) and #2 and #3");
		} else {
			filter.setMaskString("(#0 or #1) and #2");
		}
		view.setFilter(filter);
		CurProjectCollection curProjectCollection = CurProjectFactory
				.getRemoteInstance().getCurProjectCollection(view);
		CurProjectInfo element;
		for (Iterator iter = curProjectCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (CurProjectInfo) iter.next();

		return idSet;
	}

	public static Set genContractTypeIdSet(BOSUuid id) throws Exception {
		Set idSet = new HashSet();
		ContractTypeInfo contractTypeInfo = ContractTypeFactory
				.getRemoteInstance().getContractTypeInfo(new ObjectUuidPK(id));
		String longNumber = contractTypeInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber + "!%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber));
		filter.setMaskString("#0 or #1");
		view.setFilter(filter);
		ContractTypeCollection contractTypeCollection = ContractTypeFactory
				.getRemoteInstance().getContractTypeCollection(view);
		ContractTypeInfo element;
		for (Iterator iter = contractTypeCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (ContractTypeInfo) iter.next();

		idSet.add(id.toString());
		return idSet;
	}

	public static Set genTargetTypeIdSet(BOSUuid id) throws Exception {
		Set idSet = new HashSet();
		TargetTypeInfo TargetTypeInfo = TargetTypeFactory.getRemoteInstance()
				.getTargetTypeInfo(new ObjectUuidPK(id));
		String longNumber = TargetTypeInfo.getLongNumber();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber + "!%",
						CompareType.LIKE));
		filter.getFilterItems().add(
				new FilterItemInfo("longNumber", longNumber));
		filter.setMaskString("#0 or #1");
		view.setFilter(filter);
		TargetTypeCollection TargetTypeCollection = TargetTypeFactory
				.getRemoteInstance().getTargetTypeCollection(view);
		TargetTypeInfo element;
		for (Iterator iter = TargetTypeCollection.iterator(); iter.hasNext(); idSet
				.add(element.getId().toString()))
			element = (TargetTypeInfo) iter.next();

		return idSet;
	}

	public boolean isContractDetailDefUsed(String id) {
		boolean b = false;
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(new FilterItemInfo("detailDefID", id));
		try {
			b = ContractBillEntryFactory.getRemoteInstance().exists(filter);
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		return b;
	}

	public static CurProjectInfo getProjectInfoForDisp(String projId) {
		CurProjectInfo curProjectInfo = null;
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add(new SelectorItemInfo("displayName"));
		selectors.add(new SelectorItemInfo("CU.id"));
		try {
			curProjectInfo = CurProjectFactory.getRemoteInstance()
					.getCurProjectInfo(new ObjectStringPK(projId), selectors);
		} catch (EASBizException e) {
			ExceptionHandler.handle(e);
		} catch (BOSException e) {
			ExceptionHandler.handle(e);
		}
		return curProjectInfo;
	}

	public static void fmtFootNumber(KDTable table, String colNames[]) {
		if (table.getFootManager() == null
				|| table.getFootManager().getFoot() == null)
			return;
		for (int i = 0; i < colNames.length; i++)
			table.getFootRow(0).getCell(colNames[i]).getStyleAttributes()
					.setNumberFormat("#,##0.00;-#,##0.00");

	}

	public static EntityViewInfo getApprovedSupplierView() {
		EntityViewInfo v = new EntityViewInfo();
		FilterInfo f = new FilterInfo();
		f.getFilterItems()
				.add(new FilterItemInfo("usedStatus", new Integer(1)));
		v.setFilter(f);
		return v;
	}

	public static void setRespDeptF7(KDBizPromptBox bizPromptBox,
			CoreUIObject ui) {
		setRespDeptF7(bizPromptBox, ui, null);
	}

	public static void setRespDeptF7(KDBizPromptBox bizPromptBox,
			CoreUIObject ui, String cuId) {
		bizPromptBox
				.setQueryInfo("com.kingdee.eas.basedata.org.app.AdminOrgUnitQuery");
		EntityViewInfo view = new EntityViewInfo();
		SorterItemCollection sorc = view.getSorter();
		SorterItemInfo sort = new SorterItemInfo("number");
		sorc.add(sort);
		FilterInfo filter = new FilterInfo();
		FilterItemCollection fic = filter.getFilterItems();
		fic.add(new FilterItemInfo("isFreeze", new Integer(0)));
		fic.add(new FilterItemInfo("isSealUp", new Integer(0)));
		if (cuId != null)
			fic.add(new FilterItemInfo("CU.id", cuId));
		try {
			Set authorizedOrgs = new HashSet();
			Map orgs = (Map) ActionCache
					.get("FDCBillEditUIHandler.authorizedOrgs");
			if (orgs == null)
				orgs = PermissionFactory.getRemoteInstance().getAuthorizedOrgs(
						new ObjectUuidPK(SysContext.getSysContext()
								.getCurrentUserInfo().getId()), OrgType.Admin,
						null, null, null);
			if (orgs != null) {
				Set orgSet = orgs.keySet();
				for (Iterator it = orgSet.iterator(); it.hasNext(); authorizedOrgs
						.add(it.next()))
					;
			}
			FilterInfo filterID = new FilterInfo();
			filterID.getFilterItems().add(
					new FilterItemInfo("id", authorizedOrgs,
							CompareType.INCLUDE));
			filter.mergeFilter(filterID, "and");
		} catch (Exception e) {
			e.printStackTrace();
		}
		view.setFilter(filter);
		bizPromptBox.setEntityViewInfo(view);
		AdminF7 f7 = new AdminF7(ui);
		f7.showCheckBoxOfShowingAllOUs();
		f7.setRootUnitID(cuId);
		if (cuId != null)
			f7.setCurrentCUID(cuId);
		bizPromptBox.setSelector(f7);
		SelectorItemCollection sic = bizPromptBox.getSelectorCollection();
		if (sic == null) {
			sic = new SelectorItemCollection();
			sic.add(new SelectorItemInfo("number"));
			sic.add(new SelectorItemInfo("name"));
			bizPromptBox.setSelectorCollection(sic);
		}
		sic.add(new SelectorItemInfo("isLeaf"));
		sic.add(new SelectorItemInfo("displayName"));
		if (bizPromptBox.getClientProperty("RespDeptDataChangeLisenter") == null) {
			RespDeptDataChangeLisenter ls = new RespDeptDataChangeLisenter(ui);
			bizPromptBox.addDataChangeListener(ls);
			bizPromptBox.putClientProperty("RespDeptDataChangeLisenter", ls);
		}
	}

	public static void checkCurrentCostCenterOrg(CoreUIObject ui) {
		CostCenterOrgUnitInfo currentCostOrg = SysContext.getSysContext()
				.getCurrentCostUnit();
		if (currentCostOrg == null) {
			MsgBox.showWarning(ui, getRes("notCostCenter"));
			SysUtil.abort();
		}
	}

	public static void checkCurrentBizCostCenterOrg(CoreUIObject ui) {
		checkCurrentCostCenterOrg(ui);
		CostCenterOrgUnitInfo currentCostOrg = SysContext.getSysContext()
				.getCurrentCostUnit();
		if (!currentCostOrg.isIsBizUnit()) {
			MsgBox
					.showWarning(
							ui,
							"\u8BE5\u529F\u80FD\u53EA\u80FD\u5728\u5B9E\u4F53\u6210\u672C\u4E2D\u5FC3\u4F7F\u7528");
			SysUtil.abort();
		}
	}

	public static void initSupplierF7(CoreUIObject owner, KDBizPromptBox box) {
		initSupplierF7(owner, box, null);
	}

	public static void initSupplierF7(CoreUIObject owner, KDBizPromptBox box,
			String cuId) {
		GeneralKDPromptSelectorAdaptor selectorLisenterSupplier = new GeneralKDPromptSelectorAdaptor(
				box,
				"com.kingdee.eas.basedata.master.cssp.client.F7SupplierTreeDetailListUI",
				owner, CSSPGroupInfo.getBosType(),
				"com.kingdee.eas.basedata.master.cssp.app.F7SupplierQuery",
				"browseGroup.id",
				"com.kingdee.eas.basedata.master.cssp.app.F7SupplierQuery",
				"com.kingdee.eas.basedata.master.cssp.app.F7SupplierQueryWithDefaultStandard");
		if (cuId != null) {
			selectorLisenterSupplier.setCUId(cuId);
			CtrlUnitInfo ctrl = new CtrlUnitInfo();
			ctrl.setId(BOSUuid.read(cuId));
			box.setCurrentCtrlUnit(ctrl);
		}
		box.setSelector(selectorLisenterSupplier);
		box.setHasCUDefaultFilter(false);
		box.getQueryAgent().setDefaultFilterInfo(null);
		box.setEntityViewInfo(getApproved4SupplierF7Filter(cuId));
		box
				.setQueryInfo("com.kingdee.eas.basedata.master.cssp.app.F7SupplierQueryWithDefaultStandard");
	}

	public static EntityViewInfo getApproved4SupplierF7Filter(String cuId) {
		EntityViewInfo view = new EntityViewInfo();
		SorterItemCollection sorc = view.getSorter();
		SorterItemInfo sort = new SorterItemInfo("number");
		sorc.add(sort);
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems().add(
				new FilterItemInfo("usedStatus", String
						.valueOf(UsedStatusEnum.APPROVED.getValue()),
						CompareType.EQUALS));
		view.setFilter(filterInfo);
		FilterInfo Dfilter = null;
		try {
			if (!StringUtils.isEmpty(cuId))
				if (filterMap.containsKey(cuId)) {
					Dfilter = (FilterInfo) filterMap.get(cuId);
				} else {
					Dfilter = (FilterInfo) ActionCache
							.get("FDCBillEditUIHandler.dfilter");
					if (Dfilter == null)
						Dfilter = GlWebServiceUtil.getDFilterInfo(
								SupplierFactory.getRemoteInstance(), cuId);
					filterMap.put(cuId, Dfilter);
				}
			if (Dfilter != null)
				filterInfo.mergeFilter(Dfilter, "and");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}

	public static Set getProjIdsOfCurCostOrg() {
		Set ids = new HashSet();
		String curCostId = SysContext.getSysContext().getCurrentOrgUnit()
				.getId().toString();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("costCenterOU.id", curCostId));
		view.setFilter(filter);
		ProjectWithCostCenterOUCollection relations = null;
		try {
			relations = ProjectWithCostCenterOUFactory.getRemoteInstance()
					.getProjectWithCostCenterOUCollection(view);
		} catch (BOSException e) {
			e.printStackTrace();
		}
		if (relations == null || relations.size() == 0)
			return null;
		String projId = relations.get(0).getCurProject().getId().toString();
		ObjectUuidPK projUuidPK = new ObjectUuidPK(projId);
		try {
			if (!CurProjectFactory.getRemoteInstance().exists(projUuidPK))
				return null;
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		CurProjectInfo curProjectInfo = null;
		try {
			curProjectInfo = CurProjectFactory.getRemoteInstance()
					.getCurProjectInfo(projUuidPK);
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		String projNumber = curProjectInfo.getLongNumber();
		FilterInfo projFilter = new FilterInfo();
		projFilter.getFilterItems().add(
				new FilterItemInfo("longnumber", projNumber + "!%",
						CompareType.LIKE));
		projFilter.getFilterItems().add(
				new FilterItemInfo("longnumber", projNumber));
		String orgUnitId = SysContext.getSysContext().getCurrentFIUnit()
				.getId().toString();
		projFilter.getFilterItems().add(
				new FilterItemInfo("fullOrgUnit", orgUnitId));
		projFilter.setMaskString("(#0 or #1) and #2");
		view = new EntityViewInfo();
		view.setFilter(projFilter);
		view.getSelector().add("id");
		CurProjectCollection projs = new CurProjectCollection();
		try {
			projs = CurProjectFactory.getRemoteInstance()
					.getCurProjectCollection(view);
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		Iterator iter = projs.iterator();
		do {
			if (!iter.hasNext())
				break;
			CurProjectInfo element = (CurProjectInfo) iter.next();
			if (element != null) {
				String id = element.getId().toString();
				ids.add(id);
			}
		} while (true);
		return ids;
	}

	public static Set getProjIdsOfCostOrg(FullOrgUnitInfo orgUnit,
			boolean isOnlyLeaf) throws EASBizException, BOSException {
		Set ids = new HashSet();
		String curCostId = orgUnit.getId().toString();
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("costCenterOU.id", curCostId));
		view.setFilter(filter);
		ProjectWithCostCenterOUCollection relations = null;
		try {
			relations = ProjectWithCostCenterOUFactory.getRemoteInstance()
					.getProjectWithCostCenterOUCollection(view);
		} catch (BOSException e) {
			e.printStackTrace();
		}
		if (relations == null || relations.size() == 0)
			return null;
		String projId = relations.get(0).getCurProject().getId().toString();
		ObjectUuidPK projUuidPK = new ObjectUuidPK(projId);
		try {
			if (!CurProjectFactory.getRemoteInstance().exists(projUuidPK))
				return null;
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		CurProjectInfo curProjectInfo = null;
		try {
			curProjectInfo = CurProjectFactory.getRemoteInstance()
					.getCurProjectInfo(projUuidPK);
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		String projNumber = curProjectInfo.getLongNumber();
		FilterInfo projFilter = new FilterInfo();
		projFilter.getFilterItems().add(
				new FilterItemInfo("longnumber", projNumber + "!%",
						CompareType.LIKE));
		projFilter.getFilterItems().add(
				new FilterItemInfo("longnumber", projNumber));
		String orgUnitId = null;
		if (orgUnit.getPartFI() == null || orgUnit.getPartFI().getId() == null) {
			SelectorItemCollection selector = new SelectorItemCollection();
			selector.add("partFI.id");
			FullOrgUnitInfo fullOrgUnitInfo = FullOrgUnitFactory
					.getRemoteInstance().getFullOrgUnitInfo(
							new ObjectUuidPK(orgUnit.getId()), selector);
			if (fullOrgUnitInfo.getPartFI() != null)
				orgUnitId = fullOrgUnitInfo.getPartFI().getId().toString();
		} else {
			orgUnitId = orgUnit.getPartFI().getId().toString();
		}
		projFilter.getFilterItems().add(
				new FilterItemInfo("fullOrgUnit", orgUnitId));
		if (isOnlyLeaf) {
			projFilter.getFilterItems().add(
					new FilterItemInfo("isLeaf", Boolean.TRUE));
			projFilter.setMaskString("(#0 or #1) and #2 and #3");
		} else {
			projFilter.setMaskString("(#0 or #1) and #2");
		}
		view = new EntityViewInfo();
		view.setFilter(projFilter);
		view.getSelector().add("id");
		CurProjectCollection projs = new CurProjectCollection();
		try {
			projs = CurProjectFactory.getRemoteInstance()
					.getCurProjectCollection(view);
		} catch (Exception e) {
			ExceptionHandler.handle(e);
		}
		Iterator iter = projs.iterator();
		do {
			if (!iter.hasNext())
				break;
			CurProjectInfo element = (CurProjectInfo) iter.next();
			if (element != null) {
				String id = element.getId().toString();
				ids.add(id);
			}
		} while (true);
		return ids;
	}

	public static void setPersonF7(KDPromptBox box, CoreUIObject owner,
			String cuId) {
		HashMap param = new HashMap();
		if (cuId != null)
			param.put("CU_ID", cuId);
		else
			param.put("All_Admins", "YES");
		((KDBizPromptBox) box).setHasCUDefaultFilter(false);
		PersonPromptBox pmt = new PersonPromptBox(owner, param);
		box.setSelector(pmt);
		if (cuId != null) {
			EntityViewInfo view = new EntityViewInfo();
			SorterItemCollection sorc = view.getSorter();
			SorterItemInfo sort = new SorterItemInfo("number");
			sorc.add(sort);
			FilterInfo filter = new FilterInfo();
			view.setFilter(filter);
			FilterItemCollection fic = filter.getFilterItems();
			fic.add(new FilterItemInfo("CU2.id", cuId));
			fic.add(new FilterItemInfo("CU2.id", null, CompareType.NOTEQUALS));
			fic.add(new FilterItemInfo("CU.id", cuId));
			filter.setMaskString("(#0 and #1) or #2 ");
			((KDBizPromptBox) box).setEntityViewInfo(view);
		}
	}

	public static PeriodInfo getGLCurCompanyCurPeriod() {
		CompanyOrgUnitInfo curC = SysContext.getSysContext().getCurrentFIUnit();
		PeriodInfo curP = null;
		try {
			curP = SystemStatusCtrolUtils.getCurrentPeriod(null,
					SystemEnum.GENERALLEDGER, curC);
		} catch (EASBizException e) {
			ExceptionHandler.handle(e);
		} catch (BOSException e) {
			ExceptionHandler.handle(e);
		}
		return curP;
	}

	public static boolean hasMutexed(Throwable e) {
		boolean hasMutex = false;
		if (e instanceof EASBizException) {
			EASBizException e2 = (EASBizException) e;
			String error = EASResource
					.getString("com.kingdee.eas.framework.FrameWorkResource.Error_ObjectUpdateLock_Request");
			if (e2.getMessage() != null && e2.getMessage().equals(error))
				hasMutex = true;
		}
		return hasMutex;
	}

	public static void requestDataObjectLock(CoreUI ui, java.util.List idList,
			String oprtState) throws Throwable {
		String oldOprtState;
		if (ui == null || idList == null || oprtState == null)
			return;
		oldOprtState = ui.getOprtState();
		ui.setOprtState(oprtState);
		String id;
		for (Iterator iter = idList.iterator(); iter.hasNext(); ui
				.pubFireVOChangeListener(id))
			id = iter.next().toString();
		ui.setOprtState(oldOprtState);
	}

	public static void releaseDataObjectLock(CoreUI ui, java.util.List idList)
			throws Throwable {
		String oprtState = "RELEASEALL";
		requestDataObjectLock(ui, idList, oprtState);
	}

	public static final String RESOURCE = "com.kingdee.eas.fdc.basedata.client.FdcResource";
	public static Map filterMap = new HashMap();

}